/*
 * @file iso9660_dbg.c
 *
 *    ISO9660 debug handlers
 */


#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <rtems/seterr.h>

#include "iso9660.h"
#include "iso9660_dbg.h"

#if RTEMS_ISO9660_DEBUG

rtems_iso9660_dbg_mask_t rtems_iso9660_dbg_mask = RTEMS_ISO9660_DEBUG_DEFAULT;

uint8_t
rtems_iso9660_section_traced(
    rtems_iso9660_dbg_mask_t section
)
{
  return (rtems_iso9660_dbg_mask & section)?1:0;
}

void
rtems_iso9660_dbg_set_trace(
    rtems_iso9660_dbg_mask_t mask
)
{
  rtems_iso9660_dbg_mask = mask;
}

void
rtems_iso9660_dbg_section_enable(
    rtems_iso9660_dbg_mask_t section
)
{
  rtems_iso9660_dbg_mask |= section;
}

void
rtems_iso9660_dbg_section_disable(
    rtems_iso9660_dbg_mask_t section
)
{
  rtems_iso9660_dbg_mask ^= section;
}

rtems_iso9660_dbg_mask_t
rtems_iso9660_dbg_get_mask(
)
{
  return rtems_iso9660_dbg_mask;
}

void
rtems_iso9660_dbg_trace(
    rtems_iso9660_dbg_mask_t section,
    char *format,
    ...
)
{
  va_list args;

  if (rtems_iso9660_section_traced(section))
  {
    char buffer[256];

    va_start(args, format);
    vsnprintf(buffer, sizeof(buffer), format, args);
    va_end(args);

    printf("iso9660: %s: %s", "???", buffer);
  }
}

#define CHAR_PER_LINE 15

void
rtems_iso9660_dbg_string(
    char *string,
    size_t len
)
{
  int i;

  if (len == 0)
    len = strlen(string);

  for (i = 0; i < len; i += CHAR_PER_LINE)
  {
    int j;

    /* Print characters */
    for (j = 0; (j < CHAR_PER_LINE) && (i + j < len); j++)
      printf("%c ", isgraph((int)string[i+j])?string[i+j]:'.');

    /* Remaining spaces */
    for (; j < CHAR_PER_LINE; j++)
      printf("  ");

    printf(" | ");

    /* Hexadecimal values */
    for (j = 0; (j < CHAR_PER_LINE) && (i + j < len); j++)
      printf("%2.2X ", (unsigned char)string[i+j]);

    printf("\n");
  }
}

#endif /* RTEMS_ISO9660_DEBUG */
